fix(testsuite):fix flaky test by ensure port is free to use#545
fix(testsuite):fix flaky test by ensure port is free to use#545arshia-rgh wants to merge 1 commit intorootless-containers:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR attempts to fix flaky tests in the port testsuite by replacing the previous hardcoded port calculation formula (childPID + port) % 60000 with a new getFreePort function that dynamically allocates free ports using the OS's ephemeral port assignment mechanism.
Key changes:
- Added
getFreePorthelper function to obtain free ports by binding to port 0 and retrieving the OS-assigned port - Modified
testProtoWithPIDto dynamically allocate parent ports instead of using a PID-based formula - Removed the FIXME comment about flaky tests
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: arshia-rgh <arshiarezagholi1212@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| pairs := map[int]int{ | ||
| // FIXME: flaky | ||
| 80: (childPID + 80) % 60000, | ||
| 8080: (childPID + 8080) % 60000, | ||
| } | ||
| if proto == "tcp" { | ||
| for _, parentPort := range pairs { | ||
| var d net.Dialer | ||
| d.Timeout = 50 * time.Millisecond | ||
| _, err := d.Dial(proto, fmt.Sprintf("127.0.0.1:%d", parentPort)) | ||
| if err == nil { | ||
| t.Fatalf("port %d is already used?", parentPort) | ||
| } | ||
| } | ||
| 80: 0, | ||
| 8080: 0, | ||
| } |
There was a problem hiding this comment.
Setting ParentPort to 0 will cause AddPort() to fail validation. The validation in pkg/port/portutil/portutil.go:135 explicitly rejects port values <= 0:
if spec.ParentPort <= 0 || spec.ParentPort > 65535 {
return fmt.Errorf("invalid ParentPort: %q", spec.ParentPort)
}The test will fail at line 221 in testProtoRoutine when d.AddPort() returns this validation error.
To fix the flaky test, you need a different approach that ensures the port is free before using it, such as:
- Finding an available port dynamically (e.g., binding to port 0 temporarily to get an OS-assigned port, then closing and using that port number)
- Using a port allocation helper that checks availability
- Using a wider range or better randomization in the previous formula to reduce collision probability
No description provided.